package com.bkgrands.fpmu.Adapters;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bkgrands.fpmu.R;
import com.bkgrands.fpmu.activities.ProgressActivity;
import com.bkgrands.fpmu.classes.MinorStage;
import com.bkgrands.fpmu.classes.Photo;
import com.bkgrands.fpmu.classes.StaticFarm;

import java.util.List;

/**
 * Created by jeff on 7/13/19.
 */

public class MinorstagesAdapter extends RecyclerView.Adapter<MinorstagesAdapter.ViewHolder> {
    private List<MinorStage> minorStages;
    Context context;

    public MinorstagesAdapter(Context context, List<MinorStage> minstage) {
        this.minorStages = minstage;
        this.context = context;
        notifyDataSetChanged();

    }


    @Override
    public MinorstagesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.stage2, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MinorstagesAdapter.ViewHolder viewHolder, final int i) {
        viewHolder.tv_facility.setText(minorStages.get(i).getName());
        viewHolder.siteprogress.setVisibility(View.VISIBLE);
        viewHolder.siteprogress.setText(minorStages.get(i).getProgress().getStatus());
        if (viewHolder.siteprogress.getText().equals("not-started")){
            viewHolder.siteprogress.setVisibility(View.GONE);
        }

        List<Photo> mPhotos = minorStages.get(i).getProgress().getPhotos();
        ProgressAdapter horizontalAdapter = new ProgressAdapter(context,mPhotos);
        viewHolder.recyclerViewHorizontal.setAdapter(horizontalAdapter);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StaticFarm.currentMinorStage = MinorstagesAdapter.this.minorStages.get(i);
                Intent intent = new Intent(v.getContext(), ProgressActivity.class);
                intent.putExtra( "minormajor", String.valueOf(minorStages.get(i).getName()));
                intent.putExtra("progressStatus",String.valueOf(minorStages.get(i).getProgress().getStatus()));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return minorStages.size();
    }

    public void updateData(List<MinorStage> minorStages) {
        this.minorStages = minorStages;
        this.notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_facility,siteprogress;
        private RecyclerView recyclerViewHorizontal;
        private LinearLayoutManager horizontalManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);

        public ViewHolder(View view) {
            super(view);
            tv_facility = (TextView)view.findViewById(R.id.stagename);
            siteprogress = (TextView)view.findViewById(R.id.siteprogress);
            recyclerViewHorizontal = itemView.findViewById(R.id.project_progress);
            recyclerViewHorizontal.setHasFixedSize(true);
            recyclerViewHorizontal.setNestedScrollingEnabled(false);
            recyclerViewHorizontal.setLayoutManager(horizontalManager);
            recyclerViewHorizontal.setItemAnimator(new DefaultItemAnimator());
        }
    }

}
